<template>
  <div class="loginWindow">
    <div class="loginWindow_div">
      <div class="loginWindow_logo">
        <img src="@/assets/login.png">
      </div>
      <div class="loginWindow_body">
        <div class="loginWindow_system_name">通用系统</div>
        <div class="loginWindow_body_window">
          <div class="loginWindow_body_form">
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleFormRef"
                label-width="100px"
                label-position="top"
                class="demo-ruleForm"
            >
              <el-form-item label="用户名：" prop="userName">
                <el-input size="large" v-model="ruleForm.userName"></el-input>
              </el-form-item>
              <el-form-item label="密码：" prop="pwd">
                <el-input size="large" v-model="ruleForm.pwd" type="password"></el-input>
              </el-form-item>
            </el-form>
          </div>
          <div class="loginWindow_body_btn">
            <div class="loginWindow_body_btn_body" @click="submitForm()">登录</div>
            
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup name="login">
import {reactive, ref} from "vue";
import {useRouter} from 'vue-router'
import md5 from 'js-md5'
import api from '@/plugins/api/index'
import store from "@/store";
const router = useRouter();

const ruleForm = reactive({
  userName: '',
  pwd: ''
})

const ruleFormRef = ref(null)

const rules = reactive({
  userName: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ]
})

function submitForm() {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      login()
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
async function login() {
  const res = await api.login({
    userName: ruleForm.userName,
    pwd: md5(ruleForm.pwd)
  })
  if (res.data.code === 200) {
    window.sessionStorage.setItem('token', res.data.data.token)
    store.commit('updateUserInfo', res.data.data.userInfo)
    window.location.replace('/')
  }
  console.log(res)
}


</script>

<style lang="scss" scoped>
.loginWindow {
  width: 100%;
  height: 100%;
  position: fixed;
  display: flex;
  display: -webkit-flex;
  display: -moz-flex;
  align-items: center;
  justify-content: center;
  top: 0;
  left: 0;
  background-color: #eeeeee;

  .loginWindow_div {
    width: 1050px;
    height: auto;
    display: flex;
    background-color: #ffffff;
    display: -webkit-flex;
    display: -moz-flex;
    align-items: center;
    border-radius: 10px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);

    .loginWindow_logo {
      width: 500px;
      height: auto;

      & > img {
        width: 100%;
        height: auto;
      }
    }

    .loginWindow_body {
      width: calc(100% - 500px);
      box-sizing: border-box;
      padding-left: 40px;
      height: auto;
      border-left: 1px solid #eeeeee;

      .loginWindow_system_name {
        width: 100%;
        height: auto;
        font-size: 26px;
        font-weight: 700;
        margin-bottom: 20px;
      }

      .loginWindow_body_window {
        width: 500px;
        height: auto;
        box-sizing: border-box;
        padding: 20px 40px 0 0;
        //box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);

        .loginWindow_body_form {
          width: 100%;
          height: auto;
          box-sizing: border-box;
          padding: 20px 0;
        }

        .loginWindow_body_btn {
          width: 100%;
          height: auto;
          box-sizing: border-box;
          padding-top: 30px;
          //border-top: 1px solid #eeeeee;
          text-align: center;

          .loginWindow_body_btn_body {
            width: 100%;
            height: 50px;
            color: #ffffff;
            font-size: 18px;
            border-radius: 10px;
            background-color: cornflowerblue;
            text-align: center;
            line-height: 50px;
            box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
          }
        }
      }
    }
  }
}
</style>